package com.socialcoco.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
//@Deprecated
public class myDataSourceConfig {//当然这些官方的druid都有自带的,所以在yaml里面都可以配置
    @Bean
    @ConfigurationProperties("spring.datasource")//和配置文件中的数据源绑定
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource=new DruidDataSource();//当导入第三方的数据源的时候这里就可以创建
        druidDataSource.setFilters("stat,wall");//开启监控和防火墙功能，也可以在yaml里面配置
        return druidDataSource;
    }
    @Bean
    public ServletRegistrationBean statViewServlet(){//开启监控页面功能
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(statViewServlet,"/druid");
        servletRegistrationBean.addInitParameter("loginUsername","admin");//进入监控页面需要的账号密码
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        return servletRegistrationBean;
    }

}
